"""
用户接口
"""
# sfb
import json
from fastapi import APIRouter
import uuid

# zjb
from pydantic import BaseModel

from models import RoleGroup, RoleGroup_Menus, Menus, User
from schemas import RoleGroup_Pydantic, RoleGroupIn_Pydantic
from core.utils import SelfJSONResponse, SelectJSONResponse, computeMD5

role = APIRouter(tags=["角色相关"], prefix="/roleAction")


class GroupRequest(BaseModel):
    groupid: str
    description: str
    groupname: str
    menuids: str


class GroupAdd(BaseModel):
    groupid: str
    description: str
    groupname: str


@role.post("/add", summary="新增角色")
async def add_user(groupAdd: GroupAdd):
    await RoleGroup.create(groupid=groupAdd.groupid,
                           description=groupAdd.description,
                           groupname=groupAdd.groupname)

    return SelfJSONResponse(code=200,
                            msg="新增成功！")


@role.delete("/{groupid}", summary="删除角色")
async def delete_role(groupid: str):
    try:
        await RoleGroup_Menus.filter(groupid=groupid).all().delete()
        await User.filter(groupid=groupid).all().delete()
        await RoleGroup.filter(groupid=groupid).delete()
        return SelfJSONResponse(code=200, msg="删除成功！")
    except:
        return SelfJSONResponse(code=500, msg="删除失败！")


@role.put("/updateRoleGroup", summary="修改角色")
async def add_user(groupRequest: GroupRequest):
    groupid = groupRequest.groupid
    await RoleGroup_Menus.filter(groupid=groupid).all().delete()
    menuids = groupRequest.menuids.split(",")
    for menuid in menuids:
        await RoleGroup_Menus.create(groupid=groupid, menuid=menuid)

    await RoleGroup.filter(groupid=groupid).update(description=groupRequest.description,
                                                   groupname=groupRequest.groupname)

    return SelfJSONResponse(code=200,
                            msg="修改成功！")


@role.get("/list", summary="查询列表用户")
async def list_User():
    roles = await RoleGroup.all()
    rs = list()
    for roleInfo in roles:
        rs.append(roleInfo.getJson())

    return SelectJSONResponse(code=200, rows=rs,
                              total=await RoleGroup.all().count(), msg="查询成功")


async def getMenuTree():
    menusAll = await Menus.all()
    resultValue = list()
    for menu in menusAll:
        if menu.parentMenuid == None or str(menu.parentMenuid).strip() == "":
            level_1 = dict()
            level_1["id"] = menu.menuid
            level_1["label"] = menu.title
            # 二级列表
            level2List = list()
            for submenu in menusAll:
                if submenu.menuid != None and submenu.parentMenuid == menu.menuid:
                    level_2 = dict()
                    level_2["id"] = submenu.menuid
                    level_2["label"] = submenu.title
                    level2List.append(level_2)
            # 追加子路由
            level_1["children"] = level2List
            # 往结果list中追加进当前对象
            resultValue.append(level_1)
    return resultValue


@role.get("/getRoleGroupTree")
async def getRoleGroupTree(groupid: str):
    menuTree = await getMenuTree()

    allmenulist = list()
    for menu in menuTree:
        allmenulist.append(menu)

    # 选中部分
    roleList = await RoleGroup_Menus.filter(groupid=groupid).all()
    checkmenulist = list()
    for role in roleList:
        checkmenulist.append(role.getJson())

    return SelfJSONResponse(code=200, data={
        'allmenulist': allmenulist,
        'checkmenulist': checkmenulist
    }, msg="操作成功！")
